<template>
  <div>
    <el-menu
        default-active="0"
        class="el-menu-vertical-demo"
        @select="handleSelect"
        background-color="#545c64"
        text-color="#fff"
        active-text-color="#ffd04b">
      <el-menu-item index="0">
        <i class="el-icon-s-home"></i>
        <span slot="title" >主页</span>
      </el-menu-item>
      <el-menu-item index="1">
        <i class="el-icon-notebook-1"></i>
        <span>我的成绩</span>
      </el-menu-item>
      <el-menu-item index="2">
        <i class="el-icon-setting"></i>
        <span>成绩统计</span>
      </el-menu-item>
    </el-menu>
  </div>
</template>

<script>
export default {
  data(){
    return{
      user:{
        id: '',
        password:'',
        type: ''
      },
    }
  },
  created(){
    this.userCheck('');
  },
  methods: {
    handleSelect(key){
      this.userCheck();
      switch(key){
        case '0': this.userCheck('/student/main'); break;
        case '1': this.userCheck('/student/myGrades'); break;
        case '2': this.userCheck('/student/gradeStatistics'); break;
      }
    },
    userCheck(url){
      this.updateUser();

      if(this.user.id == null || this.user.type !== 'student'){
        sessionStorage.clear();
        this.$router.push('/');
      }
      else{
        let path = 'http://localhost:8081/user/' + this.user.type + 'Check';
        this.$ajax.post(path,this.user).then(res=>{
          if(res.data.status !== 0){
            sessionStorage.clear();
            alert(res.data.message + ' 请重新登录！');
            this.$router.push('/');
          }
          else{
            if(url != null && url.length > 0 && this.$route.path !== url)
              this.$router.push(url);
          }
        })
      }
    },
    updateUser(){
      this.user.id = JSON.parse(sessionStorage.getItem("id"))
      this.user.password = JSON.parse(sessionStorage.getItem("password"))
      this.user.type = JSON.parse(sessionStorage.getItem("type"))
    }
  }
}
</script>

<style scoped>

</style>